<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/7/11
 * Time: 14:12
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Serve extends Base
{
    /*
     * 服务工单首页
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        if(request()->isAjax ()){
            $serve_status = input('serve_status');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $number = input('number');
            $commit_user_keyword = input('commit_user_keyword');

            $where = [];

            if(isset($serve_status) && $serve_status != ''){
                $where['serve_status'] = $serve_status;
            }
            if(isset($number) && $number != ''){
                $where['serve_number'] = ['like', '%' . $number . '%'];
            }
            if(isset($commit_user_keyword) && $commit_user_keyword != ''){
                $where['u1.user_real_name|u1.user_phone'] = ['like', '%' . $commit_user_keyword . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['serve_commit_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['serve_commit_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['serve_commit_time'] = ['<=', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count2 = Db::table('up_serve')
                ->join('up_u_user u1', 'u1.user_id = serve_user_id', 'LEFT')
                ->join('up_u_user u2', 'u2.user_id = serve_worker', 'LEFT')
                ->join('up_area ua1', 'serve_province = ua1.district_id', 'LEFT')
                ->join('up_area ua2', 'serve_city = ua2.district_id', 'LEFT')
                ->join('up_area ua3', 'serve_district = ua3.district_id', 'LEFT')
                ->where($where)
                ->column('serve_id');
            $lists = Db::table('up_serve')
                ->join('up_u_user u1', 'u1.user_id = serve_user_id', 'LEFT')
                ->join('up_u_user u2', 'u2.user_id = serve_worker', 'LEFT')
                ->join('up_area ua1', 'serve_province = ua1.district_id', 'LEFT')
                ->join('up_area ua2', 'serve_city = ua2.district_id', 'LEFT')
                ->join('up_area ua3', 'serve_district = ua3.district_id', 'LEFT')
                ->field([
                    'serve_id',
                    'serve_have_store',
                    'concat(ua1.district, ua2.district, ua3.district, serve_address) address',
                    'serve_user_name',
                    'serve_user_phone',
                    'serve_begin_date',
                    'serve_commit_time',
                    'u1.user_real_name',
                    'u1.user_phone',
                    'u2.user_username worker_name',
                    'u2.user_phone worker_phone',
                    'serve_number',
                    'serve_status',
                    'serve_tc_user_id',
                    'serve_tc_admin_id'
                ])
                ->where($where)
                ->order('serve_commit_time desc')
                ->page($Nowpage,$limits)
                ->select();

            $count = count($count2);
            $str = '';
            if($count2){
                $str = implode(',', $count2);
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists, 'count2' => $str]);
        }
        return $this->fetch();
    }

    /*
     * 服务工单详情
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function serveDetail(){
        $serve_id = input('serve_id');
        $data = Db::table('up_serve')
            ->join('up_u_user u1', 'u1.user_id = serve_user_id', 'LEFT')
            ->join('up_u_user u2', 'u2.user_id = serve_worker', 'LEFT')
            ->join('up_area ua1', 'serve_province = ua1.district_id', 'LEFT')
            ->join('up_area ua2', 'serve_city = ua2.district_id', 'LEFT')
            ->join('up_area ua3', 'serve_district = ua3.district_id', 'LEFT')
            ->field([
                'serve_id',
                'serve_have_store',
                'concat(ua1.district, ua2.district, ua3.district, serve_address) address',
                'serve_user_name',
                'serve_user_phone',
                'serve_activity_intro',
                'serve_begin_date',
                'serve_commit_time',
                'u1.user_real_name',
                'u1.user_phone',
                'u2.user_username worker_name',
                'u2.user_phone worker_phone',
                'serve_status',
                'serve_produce_price',
                'serve_jishu_price',
                'serve_taoc_price',
                'serve_notc_price',
                'serve_other_price',
                'serve_total_price',
                'serve_appraise_intro',
                'serve_appraise_images',
                'serve_star',
                'serve_real_produce_price',
                'serve_real_jishu_price',
                'serve_real_taoc_price',
                'serve_real_notc_price',
                'serve_real_other_price',
                'serve_real_total_price',
                'serve_produce_ratio',
                'serve_jishu_ratio',
                'serve_taoc_ratio',
                'serve_other_ratio',
                'serve_star_ratio',
                'serve_remark',
                'serve_td_price',
                'serve_fp_time',
                'serve_accept_time',
                'serve_done_time',
                'serve_appraise_time',
                'serve_over_time',
                'serve_number',
                'serve_cancel_time',
                'serve_tc_user_id',
                'serve_tc_admin_id'
            ])
            ->where('serve_id', $serve_id)
            ->find();
        if($data['serve_star']){
            $data['serve_star'] = json_decode($data['serve_star'], true);
        }else{
            $data['serve_star'] = [];
        }
        if($data['serve_appraise_images']){
            $data['serve_appraise_images'] = explode(',', $data['serve_appraise_images']);
        }else{
            $data['serve_appraise_images'] = [];
        }
        return $this->fetch('', ['data' => $data]);
    }

    /*
     * 分配服务老师
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function fenPei(){
        $serve_id = input('serve_id');
        if(Request::instance()->isPost()){
            $serve_user_id = input('serve_user_id');

            $serve_user = Db::table('up_u_user')->where('user_id', $serve_user_id)->field('user_serve_rule')->find();
            if(!$serve_user || ($serve_user && $serve_user['user_serve_rule'] == 2)) return json(['code' => 100, 'msg' => '该用户暂无服务权限']);

            $update = [
                'serve_id' => $serve_id,
                'serve_worker' => $serve_user_id,
                'serve_status' => 2,
                'serve_fp_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_serve')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $user = Db::table('up_u_user')->where(['user_status' => 1, 'user_serve_rule' => 1])->field('user_id, user_real_name, user_phone')->select();
        $serve = Db::table('up_serve')->where('serve_id', $serve_id)->field('serve_worker')->find();
        $this->assign([
            'serve_id' => $serve_id,
            'user' => $user,
            'serve' => $serve
        ]);
        return $this->fetch();
    }

    /*
     * 提成
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function tc(){
        $serve_id = input('serve_id');
        if(Request::instance()->isPost()){
            $real_produce_price = input('real_produce_price') ?: 0;
            $real_jishu_price = input('real_jishu_price') ?: 0;
            $real_taoc_price = input('real_taoc_price') ?: 0;
            $real_notc_price = input('real_notc_price') ?: 0;
            $real_other_price = input('real_other_price') ?: 0;
            $produce_ratio = input('produce_ratio') ?: 0;
            $jishu_ratio = input('jishu_ratio') ?: 0;
            $taoc_ratio = input('taoc_ratio') ?: 0;
            $other_ratio = input('other_ratio') ?: 0;
            $serve_remark = input('serve_remark');

            $serve = Db::table('up_serve')
                ->where('serve_id', $serve_id)
                ->field('serve_status, serve_star_ratio, serve_worker, serve_user_id')
                ->find();
            if($serve['serve_status'] != 6) return json(['code' => 100, 'msg' => '服务工单状态异常']);

            // 提成金额
            $produce_price = $real_produce_price * $produce_ratio;
            $jishu_price = $real_jishu_price * $jishu_ratio;
            $taoc_price = $real_taoc_price * $taoc_ratio;
            $other_price = $real_other_price * $other_ratio;
            $tc_price = ($produce_price * 100 + $jishu_price * 100 + $taoc_price * 100 + $other_price * 100) * $serve['serve_star_ratio'] / 10000;
            $total_price = $real_produce_price + $real_jishu_price + $real_taoc_price + $real_notc_price + $real_other_price;

            $update = [
                'serve_id' => $serve_id,
                'serve_real_produce_price' => $real_produce_price,
                'serve_real_jishu_price' => $real_jishu_price,
                'serve_real_taoc_price' => $real_taoc_price,
                'serve_real_notc_price' => $real_notc_price,
                'serve_real_other_price' => $real_other_price,
                'serve_real_total_price' => $total_price,
                'serve_produce_ratio' => $produce_ratio,
                'serve_jishu_ratio' => $jishu_ratio,
                'serve_taoc_ratio' => $taoc_ratio,
                'serve_other_ratio' => $other_ratio,
                'serve_remark' => $serve_remark,
                'serve_td_price' => $tc_price,
                'serve_status' => 7,
                'serve_over_time' => date('Y-m-d H:i:s'),
                'serve_tc_admin_id' => session('uid')
            ];

            $serve_user = Db::table('up_u_user')->where('user_id', $serve['serve_worker'])->find();

            Db::startTrans();
            try{
                // 更新服务工单
                Db::table('up_serve')->update($update);

                // 用户佣金提成
                $this->insertBoFy($serve['serve_worker'], $serve['serve_user_id'], $serve['serve_worker'], 0, 1, '服务费提点', 1, $tc_price, $serve_user['user_bo_fy_balance'], 0, 2, 0);


                // 用户佣金
                Db::table('up_u_user')
                    ->where('user_id', $serve['serve_worker'])
                    ->inc('user_bo_fy_balance', $tc_price)
                    ->inc('user_bo_total_fy_balance', $tc_price)
                    ->update();

                Db::commit();
                return json(['code' => 200, 'msg' => '操作成功']);
            }catch (\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }
        }
        $serve = Db::table('up_serve')
            ->where('serve_id', $serve_id)
            ->field([
                'serve_star_ratio',
                'serve_produce_price',
                'serve_jishu_price',
                'serve_taoc_price',
                'serve_notc_price',
                'serve_other_price',
                'serve_total_price'
            ])
            ->find();
        $product_ratio = Db::table('up_bo_fy_config')->where('bfc_id', 37)->value('bfc_content');
        $jishu_ratio = Db::table('up_bo_fy_config')->where('bfc_id', 38)->value('bfc_content');
        $taoc_ratio = Db::table('up_bo_fy_config')->where('bfc_id', 18)->value('bfc_content');
        $other_ratio = Db::table('up_bo_fy_config')->where('bfc_id', 39)->value('bfc_content');
        $td = ($serve['serve_produce_price'] * $product_ratio + $serve['serve_jishu_price'] * $jishu_ratio + $serve['serve_taoc_price'] * $taoc_ratio + $serve['serve_other_price'] * $other_ratio) / 100 * $serve['serve_star_ratio'];
        $this->assign([
            'serve_id' => $serve_id,
            'serve' => $serve,
            'product_ratio' => $product_ratio,
            'jishu_ratio' => $jishu_ratio,
            'taoc_ratio' => $taoc_ratio,
            'other_ratio' => $other_ratio,
            'td' => $td
        ]);
        return $this->fetch();
    }

    public function outExcel(){

    }
}